﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.Controls;
using System.Drawing;

namespace ShopOnline
{
    public partial class Search : BasePage
    {
        string GetCatID()
        {
            string catparm = Request.QueryString[CAT];
            int lastIndex = catparm.LastIndexOf("_");
            return catparm.Substring(lastIndex + 1);
        }

        protected void productList_OnCartChanged(object sender, EventArgs e)
        {
            ((ProductMaster)this.Master).SetCartTitle(string.Format("You have <a href='Cart.aspx'>{0} item(s)</a> in your shopping cart.", BasePage.SelectedItems.RowCount));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BannerKey = ShopOnline.BLL.BannerKey.ProductPages;
                SearchFilter = "";
                ((ProductMaster)this.Master).SetPageTitle("Search");

                if (!string.IsNullOrEmpty(Request.QueryString[TAG]))
                {
                    ProductTags tags = ProductTags.GetTags(Request.QueryString[TAG]);
                    string ids = "-1";
                    for (int i = 0; i < tags.RowCount; i++)
                    {
                        ids += "," + tags.s_ProductID;
                        tags.MoveNext();
                    }
                    SearchFilter = View_WebProducts.ColumnNames.ID + " in (" + ids + ")";
                    ((ProductMaster)this.Master).SetPageTitle("Products by Tag");
                }
                else if (!string.IsNullOrEmpty(Request.QueryString[CAT]) && Request.QueryString[CAT] == DAILY_SPECIAL)
                {
                    SearchFilter = View_WebProducts.ColumnNames.IsHotProduct + "=1";
                    ((ProductMaster)this.Master).SetPageTitle("Daily Special");
                }
                else if (!string.IsNullOrEmpty(Request.QueryString[CAT]) && Request.QueryString[CAT] == NEW_ARRIVAL)
                {
                    SearchFilter = View_WebProducts.ColumnNames.IsNewArrival + "=1";
                    ((ProductMaster)this.Master).SetPageTitle("New Arrival");
                }
                else if (!string.IsNullOrEmpty(Request.QueryString[CAT]))
                {
                    SearchFilter = View_WebProducts.ColumnNames.CategoryPath + " LIKE '%/" + GetCatID() + "/%'";
                    Categories c = new Categories();
                    c.LoadByPrimaryKey(int.Parse(GetCatID()));
                    if (c.RowCount > 0)
                    {
                        ((ProductMaster)this.Master).SetPageTitle(c.s_CategoryName);

                    }
                }

                if (!string.IsNullOrEmpty(Request.QueryString[KEY]))
                {
                    SearchFilter = View_WebProducts.ColumnNames.ProductName + " LIKE '%" + Request.QueryString[KEY] + "%' OR " + View_WebProducts.ColumnNames.CategoryName + " LIKE '%" + Request.QueryString[KEY] + "%'";
                }

                if (View_Customers.GetCurrentUser() == null || View_Customers.GetCurrentUser().DealerPrice == false)
                {
                    if(SearchFilter != "")
                        SearchFilter += " and " + View_WebProducts.ColumnNames.IsDealerOnly + " != 1";
                    else
                        SearchFilter = View_WebProducts.ColumnNames.IsDealerOnly + " != 1";
                }

                SortDir = SORT_ASC;
                productList.Rebind();
            }
        }

        public string SearchFilter
        {
            get { return ViewState["SearchFilter"].ToString();}
            set { ViewState["SearchFilter"] = value; }
        }

        public string SortExpression
        {
            get { return SortBy + SortDir; }
        }

        public string SortBy
        {
            get { return ddlSortBy.SelectedValue; }
            set { ddlSortBy.SelectedValue = value; ddlSortByDown.SelectedValue = value; }
        }

        public string SortDir
        {
            get { return ViewState["SortDir"].ToString(); }
            set { ViewState["SortDir"] = value; }
        }

        protected void btnSortDir_OnClick(object sender, EventArgs e)
        {
            if (SortDir == SORT_ASC)
            {
                SortDir = SORT_DESC;
                imgSortBy.ImageUrl = "~/images/i_desc_arrow.gif";
                imgSortDirDown.ImageUrl = "~/images/i_desc_arrow.gif";
            }
            else
            {
                SortDir = SORT_ASC;
                imgSortBy.ImageUrl = "~/images/i_asc_arrow.gif";
                imgSortDirDown.ImageUrl = "~/images/i_asc_arrow.gif";
            }

            productList.Rebind();
        }

        int PageSize
        {
            get { return dataPager.PageSize; }
            set
            {
                dataPager.PageSize = value;
                dataPagerDown.PageSize = value;
                ddlItemPerRow.SelectedValue = value.ToString();
                ddlItemPerRowDown.SelectedValue = value.ToString();
            }
        }

        int CurrentPageIndex
        {
            get { return dataPager.CurrentPageIndex; }
            set
            {
                dataPager.CurrentPageIndex = value;
                dataPagerDown.CurrentPageIndex = value;
            }
        }

        int TotalItems
        {
            get { return dataPager.TotalItems; }
            set
            {
                dataPager.TotalItems = value;
                dataPagerDown.TotalItems = value;
                lblTotalItems.Text = string.Format("{0} Item(s)", value);
                lblTotalItemsDown.Text = string.Format("{0} Item(s)", value);
            }
        }

        protected void ddlItemPerRow_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            PageSize = int.Parse(ddl.SelectedValue);
            productList.Rebind();
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            DataPagerCtrl pager = (DataPagerCtrl)sender;
            CurrentPageIndex = pager.CurrentPageIndex;
            productList.Rebind();
        }

        protected void productList_OnNeedDataSource(object sender, EventArgs e)
        {
            View_WebProducts prods = new View_WebProducts();
            int count = prods.Search(SearchFilter, SortExpression, CurrentPageIndex * PageSize, PageSize);
            productList.DataSource = prods.DefaultView;
            TotalItems = count;
            lblNoRecord.Visible = count <= 0;
        }

        protected void ddlSortBy_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            SortBy = ddl.SelectedValue;
            productList.Rebind();
        }
    }
}